<script setup lang="ts">
import { ref } from 'vue';

import { useVbenModal } from '@vben/common-ui';

import { message } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import {
  qrtzJobAddApi,
  qrtzJobEditApi,
  qrtzJobQueryByIdApi,
} from '#/api/quartz/qrtzJob';
import { queryDictOptionsApi } from '#/utils';

const record = ref({});
const [Form, formApi] = useVbenForm({
  layout: 'horizontal',
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-2',
  showDefaultActions: false,
  commonConfig: {
    colon: true,
    labelWidth: 100,
    componentProps: {
      class: 'w-full',
    },
  },
  schema: [
    {
      component: 'Input',
      label: '任务名称',
      fieldName: 'jobName',
      rules: 'required',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入任务名称',
      },
    },
    {
      component: 'Input',
      label: '调用方法',
      rules: 'required',
      fieldName: 'invokeTarget',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入调用目标字符串',
      },
    },
    {
      component: 'Input',
      label: 'cron表达式',
      fieldName: 'cronExpression',
      rules: 'required',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入cron执行表达式',
      },
    },
    {
      component: 'ApiRadioGroup',
      label: '执行策略',
      defaultValue: 0,
      fieldName: 'misfirePolicy',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        afterFetch: (data) => {
          return data.map((item: any) => ({
            label: item.itemText,
            value: Number.parseInt(item.itemVal),
          }));
        },
        api: queryDictOptionsApi,
        params: {
          dictCode: 'qrtz_execute_tactics',
        },
      },
    },
    {
      component: 'ApiRadioGroup',
      label: '并发执行',
      defaultValue: 1,
      fieldName: 'concurrent',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        afterFetch: (data) => {
          return data.map((item: any) => ({
            label: item.itemText,
            value: Number.parseInt(item.itemVal),
          }));
        },
        api: queryDictOptionsApi,
        params: {
          dictCode: 'yes_or_no',
        },
      },
    },
    {
      component: 'ApiRadioGroup',
      label: '状态',
      defaultValue: 0,
      fieldName: 'status',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        afterFetch: (data) => {
          return data.map((item: any) => ({
            label: item.itemText,
            value: Number.parseInt(item.itemVal),
          }));
        },
        api: queryDictOptionsApi,
        params: {
          dictCode: 'status',
        },
      },
    },
    {
      component: 'Textarea',
      label: '备注信息',
      fieldName: 'remark',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入备注信息',
      },
    },
  ],
});

const [Modal, modalApi] = useVbenModal({
  title: 'Modal',
  onCancel: () => {
    modalApi.close();
  },
  onConfirm: onSubmit,
  onOpenChange(isOpen: boolean) {
    if (isOpen) {
      record.value = modalApi.getData<Record<string, any>>();
      modalApi.setState({
        title: `${record.value.id ? '编辑' : '新增'}定时任务`,
      });
      if (record.value.id) {
        initFun();
      }
    }
  },
});

async function initFun() {
  const res = await qrtzJobQueryByIdApi({ id: record.value.id });
  await formApi.setValues(res, false);
}
async function onSubmit() {
  const { valid } = await formApi.validate();
  const values = await formApi.getValues();
  if (valid) {
    if (record.value.id) {
      const msg = await qrtzJobEditApi(values);
      message.success(msg);
      await modalApi.close();
    } else {
      const msg = await qrtzJobAddApi(values);
      message.success(msg);
      await modalApi.close();
    }
  }
}
</script>

<template>
  <Modal>
    <Form />
  </Modal>
</template>
